package com.tools.controller.license;


import com.tools.common.enums.ResultCode;
import com.tools.common.result.Result;
import com.tools.pojo.license.License;
import com.tools.pojo.license.LicenseVerifyResult;
import com.tools.service.license.KeyManagementService;
import com.tools.service.license.LicenseService;
import com.tools.utils.HardwareUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/license")
@Tag(name = "许可证模块",description = "许可证模块")
public class LicenseController {

    @Resource
    private LicenseService licenseService;

    @Resource
    private KeyManagementService keyManagementService;

    @Resource
    private HardwareUtil hardwareUtil;


    /**
     * 生成新的密钥对
     */
    @PostMapping("/keys/generate")
    @Operation(summary = "生成新的密钥对", description = "生成新的密钥对")
    public Result<Map<String, String>> generateKeys() {
        try {
            Map<String, String> keys = keyManagementService.generateKeyPair();
            return Result.success(keys);
        } catch (Exception e) {
            return Result.fail(ResultCode.KEY_ERROR);
        }
    }

    /**
     * 加载密钥
     */
    @PostMapping("/keys/load")
    @Operation(summary = "加载密钥", description = "加载密钥对")
    public Result<?> loadKeys(@RequestBody Map<String, String> request) {
        try {
            String privateKey = request.get("privateKey");
            String publicKey = request.get("publicKey");

            if (privateKey != null && !privateKey.trim().isEmpty()) {
                keyManagementService.loadPrivateKey(privateKey);
            }

            if (publicKey != null && !publicKey.trim().isEmpty()) {
                keyManagementService.loadPublicKey(publicKey);
            }

            return Result.success(ResultCode.KEY_LOAD_SUCCESS);
        } catch (Exception e) {
            return Result.fail(ResultCode.KEY_LOAD_ERROR);
        }
    }

    /**
     * 生成许可证
     */
    @PostMapping("/license/generate")
    @Operation(summary = "生成许可证", description = "根据输入的许可证信息生成许可证")
    public Result<?> generateLicense(@RequestBody License license) {
        try {
            if (!keyManagementService.isKeysLoaded()) {
                return Result.fail(ResultCode.KEY_GENERATE_SUCCESS);
            }

            String licenseJson = licenseService.generateLicense(license, keyManagementService.getCachedPrivateKey());

            return Result.success(licenseJson);
        } catch (Exception e) {
            return Result.fail(ResultCode.LICENSE_GENERATE_ERROR, e.getMessage());
        }
    }

    /**
     * 验证许可证
     */
    @PostMapping("/license/verify")
    @Operation(summary = "验证许可证", description = "根据输入的许可证JSON字符串验证许可证")
    public Result<?> verifyLicense(@RequestBody Map<String, String> request) {
        try {
            String licenseJson = request.get("licenseJson");

            if (!keyManagementService.isKeysLoaded()) {
                return Result.fail(ResultCode.KEY_PUBLIC_ERROR);
            }

            LicenseVerifyResult result = licenseService.verifyLicense(
                    licenseJson, keyManagementService.getCachedPublicKey());
            return result.isValid()?Result.success(result.getLicense()):Result.fail(result.getMessage());
        } catch (Exception e) {
            return Result.fail(ResultCode.LICENSE_VERIFY_ERROR, e.getMessage());
        }
    }

    /**
     * 获取当前硬件信息
     */
    @GetMapping("/hardware/info")
    @Operation(summary = "获取当前硬件信息", description = "获取当前系统的主板序列号和系统信息摘要")
    public Result<?> getHardwareInfo() {

        Map<String, String> hardwareInfo = new HashMap<>();
        hardwareInfo.put("motherboardSerial", hardwareUtil.getMotherboardSerial());
        hardwareInfo.put("systemInfo", hardwareUtil.getSystemInfo());

        return Result.success(hardwareInfo);
    }

    /**
     * 检查密钥状态
     */
    @GetMapping("/keys/status")
    @Operation(summary = "检查密钥状态", description = "检查是否已加载密钥对")
    public Result<?> getKeysStatus() {
        return Result.success( keyManagementService.isKeysLoaded());
    }

}
